window.onload=function(){

	let page = window.location.href.split('/')[window.location.href.split('/').length-1]
	$("#currentPage").html(page)
	getEmail(page)
	.then((data)=>{
		console.log(data)
		$("#allPage").html(Math.ceil(data.count/10))
		let html = ''
		for(let i of data.rows){
			html+=`<a href="/content/${i.id}" class="list-group-item">
						<h3 class="list-group-item-heading">
							${i.title}
						</h3>
						<ul class="list-unstyled">
							<li>${i.time}</li>
							<li>${i.url}</li>
						</ul>
						<ul class="hasList">`
			let to = i.to.split(',')
			if (to.length<=2) {
				for(let x of to){
					html+=`<li>${x}</li>`
				}
			}else{
				html+=`<li>${to[0]}</li><li>${to[1]}...</li><li>...</li>`
			}
			html+=`
						</ul>
					</a>`
		}
		$("#conText").html(html)
	})

	$("#upOne").click(function(){
		if (page>1) {
			window.location.href=`/${parseInt(page)-1}`
		}
	})

	$("#downOne").click(function(){
		if (page<parseInt($("#allPage").html())) {
			window.location.href=`/${parseInt(page)+1}`
		}
	})
}


function getEmail(pages){
	return new Promise((resolve,reject)=>{
		$.ajax({
			type:"post",
			url:`/getEmail/${pages}`,
			dataType:"json",
			success: function (data) {
				if (data.status==1) {
					resolve(data.result)
				}else{
					console.log(data.msg)
				}
			},
			error: function(){
				alert("请求出错");
			}
		});
	})
	
}